﻿using Framework;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;

namespace SchemaMonitor.xml
{
    [DataContract]
    [KnownType(typeof(CSms))]
    public class CMms : CSms
    {
        public List<CAddr> Addresses;
        public List<CImg> Images;
        public List<Tuple<string, string>> Other;
        public string Text;

        public CMms(XmlNode i) : base(i)
        {
            var addrs = CXml.ChildNode(i, "addrs");
            if (null != addrs)
            {
                var addr = CXml.ChildNodes(addrs, "addr");
                Addresses = new List<CAddr>(addr.Count);
                foreach (var j in addr)
                {
                    var a = new CAddr(j);
                    if (a.Address != "insert-address-token")
                        Addresses.Add(new CAddr(j));
                }
            }

            this.Images = new List<CImg>();
            this.Other = new List<Tuple<string, string>>();


            var parts = CXml.ChildNode(i, "parts");
            var part = CXml.ChildNodes(parts, "part");
            foreach (var j in part)
            {
                var text = new List<string>();
                var d = new List<Tuple<string, string>>();
                var ct = CXml.AttributeStr(j, "ct");
                Console.WriteLine(ct);
                switch (ct)
                {

                    case "image/png":
                    case "image/jpg":
                    case "image/jpeg":
                    case "video/3gpp":
                        this.Images.Add(new CImg(j));
                        break;

                    case "text/x-vCard":
                        this.Images.Add(new CVCard(j));
                        break;

                    case "text/plain":
                        text.Add(CXml.AttributeStr(j, "text"));
                        break;

                    //case "application/smil":
                    //    break;
					case "application/smil":
						foreach (XmlAttribute k in j.Attributes)
							if (k.Value.Length > 0 && "null" != k.Value)
								this.Other.Add(new Tuple<string, string>(k.Name, k.Value));
						break;

					default:
                        foreach (XmlAttribute k in j.Attributes)
							if (k.Value.Length > 0 && "null" != k.Value)
								this.Other.Add(new Tuple<string, string>(k.Name, k.Value));
                        break;

                }
                if (text.Count > 0)
                {
                    var sb = new StringBuilder();
                    foreach (var k in text)
                        sb.Append(k);
                    this.Text = sb.ToString();
                }
                if (string.IsNullOrEmpty(Body) && !string.IsNullOrEmpty(Text))
                {
                    Body = Text;
                    Text = null;
                }
            }
        }

        protected override CNameValueList GetData()
        {
            var nv = base.GetData();
            nv["IsMms", true].Value = true;

            if (!string.IsNullOrEmpty(Text))         nv.Add("Text", Text);
            if (null != Images && Images.Count > 0)  nv.Add("Images", CProto.Serialise(Images));
            if (null != Other  && Other.Count  > 0)  nv.Add("OtherProtoGz", CBinary.Zip(CProto.Serialise(Other)));
            if (null != Addresses && Addresses.Count > 0)
            {
                var addr = new List<string>();
                foreach (var i in Addresses)
                    addr.Add(i.Address);
                nv.Add("Addresses", CUtilities.ListToString(addr));
            }


            return nv;
        }

    }
}
